<!--
 * @Description: 描述
 * @Author: liym
 * @Date: 2025-05-08 20:14:38
 * @LastEditors: liym
 * @LastEditTime: 2025-05-12 15:37:43
-->
<template>
  <view class="order-list">
    <!-- <uni-search-bar @confirm="searchOrders" placeholder="输入订单ID搜索" /> -->
    <uni-table>
      <uni-tr>
        <uni-th>订单ID</uni-th>
        <uni-th>金额</uni-th>
        <uni-th>时间</uni-th>
      </uni-tr>
      <uni-tr v-for="order in orders" :key="order.id">
        <uni-td>{{ order.id }}</uni-td>
        <uni-td>{{ order.amount }} 元</uni-td>
        <uni-td>{{ order.orderTime | formatDate }}</uni-td>
      </uni-tr>
    </uni-table>
    <uni-pagination :total="total" :current="page" @change="handlePageChange" />
  </view>
</template>

<script>

import { agentRedirectionMixin } from '@/common/commonMixins.js';
import { getOrdersApi, updateOrderApi, addOrderApi, deleteOrderApi, getAgentsApi } from '@/api';
export default {
  mixins: [agentRedirectionMixin],
  data() {
    return {
      orders: [],
      page: 1,
      total: 0
    };
  },
  filters: {
    formatDate: (date, format = 'yyyy-MM-dd HH:mm:ss') => {
      if (!(date instanceof Date)) {
        date = new Date(date)  // 支持时间戳或字符串
      }

      const pad = n => n.toString().padStart(2, '0')

      const map = {
        yyyy: date.getFullYear(),
        MM: pad(date.getMonth() + 1),
        dd: pad(date.getDate()),
        HH: pad(date.getHours()),
        mm: pad(date.getMinutes()),
        ss: pad(date.getSeconds())
      }

      return format.replace(/yyyy|MM|dd|HH|mm|ss/g, match => map[match])
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    async fetchOrders() {
      const res = await uni.request({
        url: '/agent/orders',
        data: { page: this.page }
      });
      this.orders = res.data.list;
      this.total = res.data.total;
    },
    handlePageChange(e) {
      this.page = e.current;
      this.getList();
    },
    async getList () {

      uni.showLoading({
						title: 'loading',
						mask: true
      });
      const user = uni.getStorageSync('user')
      try {
        const {data} = await getOrdersApi({
          currentPage: this.page,
          agentId:user.id,pageSize: 10
        })

        this.orders = data.records || []
        this.total = data.total

      } finally {
        uni.hideLoading()
      }
    }
  }
};
</script>